package cc.rengu.igas.channel.upac.biz.impl;

import cc.rengu.igas.channel.upac.common.constant.UpacServiceConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.facade.UpacGatewayService;
import cc.rengu.igas.channel.upac.facade.request.*;
import cc.rengu.igas.channel.upac.facade.response.*;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class UpacGatewayServiceImpl implements UpacGatewayService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<B2cConsumeCancelResponse> b2cConsumeCancel(B2cConsumeCancelRequest b2cConsumeCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2cConsumeCancelResponse> result = new Result<>();
        B2cConsumeCancelResponse b2cConsumeCancelResponse = new B2cConsumeCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2cConsumeCancelRequest,
                    UpacServiceConstant.UPAC_B2C_CONSUME_CANCEL_SERVICE, B2cConsumeCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2cConsumeCancelResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("个人网关消费撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2cConsumeCancelResponse.setRespDesc(((BizException) e).getCode());
                b2cConsumeCancelResponse.setRespDesc(e.getMessage());
            } else {
                b2cConsumeCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2cConsumeCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2cConsumeRefundResponse> b2cConsumeRefund(B2cConsumeRefundRequest b2cConsumeRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2cConsumeRefundResponse> result = new Result<>();
        B2cConsumeRefundResponse b2cConsumeRefundResponse = new B2cConsumeRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2cConsumeRefundRequest,
                    UpacServiceConstant.UPAC_B2C_CONSUME_REFUND_SERVICE, B2cConsumeRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2cConsumeRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("个人网关消费退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2cConsumeRefundResponse.setRespDesc(((BizException) e).getCode());
                b2cConsumeRefundResponse.setRespDesc(e.getMessage());
            } else {
                b2cConsumeRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2cConsumeRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2bConsumeRefundResponse> b2bConsumeRefund(B2bConsumeRefundRequest b2bConsumeRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2bConsumeRefundResponse> result = new Result<>();
        B2bConsumeRefundResponse b2bConsumeRefundResponse = new B2bConsumeRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2bConsumeRefundRequest,
                    UpacServiceConstant.UPAC_B2B_CONSUME_REFUND_SERVICE, B2bConsumeRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2bConsumeRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("企业网关消费退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2bConsumeRefundResponse.setRespDesc(((BizException) e).getCode());
                b2bConsumeRefundResponse.setRespDesc(e.getMessage());
            } else {
                b2bConsumeRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2bConsumeRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2cTransStateQueryResponse> b2cTransStateQuery(B2cTransStateQueryRequest b2cTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2cTransStateQueryResponse> result = new Result<>();
        B2cTransStateQueryResponse b2cTransStateQueryResponse = new B2cTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2cTransStateQueryRequest,
                    UpacServiceConstant.B2C_TRANSTATE_QUERY_SERVICE, B2cTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2cTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("个人网关交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2cTransStateQueryResponse.setRespDesc(((BizException) e).getCode());
                b2cTransStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                b2cTransStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2cTransStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<B2bTransStateQueryResponse> b2bTransStateQuery(B2bTransStateQueryRequest b2bTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<B2bTransStateQueryResponse> result = new Result<>();
        B2bTransStateQueryResponse b2bTransStateQueryResponse = new B2bTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(b2bTransStateQueryRequest,
                    UpacServiceConstant.B2B_TRANSTATE_QUERY_SERVICE, B2bTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((B2bTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("企业网关交易状态查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                b2bTransStateQueryResponse.setRespDesc(((BizException) e).getCode());
                b2bTransStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                b2bTransStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                b2bTransStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
